home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / ACORNUSERS / EMULATOR / MAGICKIT / assembler / c / assemble next >
Text File  |  1998-04-14  |  4KB  |  211 lines

  1. #include <stdio.h>
  2. #include "defs.h"
  3. #include "externs.h"
  4.  
  5. /* translate source line to machine language */
  6.  
  7. assemble()
  8. {
  9.     struct t_line *ptr;
  10.     char *buf;
  11.     char c;
  12.     int     flag;
  13.     int     i, j;        /* prlnbuf pointer */
  14.  
  15.     /* -- Macro definition. */
  16.  
  17.     if (in_macro) {
  18.         i = SFIELD;
  19.         if (colsym(&i))
  20.             if (prlnbuf[i] == ':')
  21.                 i++;
  22.         while (isspace(prlnbuf[i]))
  23.             i++;
  24.         if (pass == LAST_PASS)
  25.             println();
  26.         if (oplook(&i) >= 0) {
  27.             if (opflg == PSEUDO) {
  28.                 if (opval == 11) {
  29.                     error("Can not nest macro definitions!");
  30.                     return;
  31.                 }
  32.                 if (opval == 12) {
  33.                     if (!check_eol(&i))
  34.                         return;
  35.                     in_macro = 0;
  36.                     return;
  37.                 }
  38.             }
  39.         }
  40.         if (pass == FIRST_PASS) {
  41.             ptr = (void *)malloc(sizeof(struct t_line));
  42.             buf = (void *)malloc(strlen(&prlnbuf[SFIELD]) + 1);
  43.             if ((ptr == NULL) || (buf == NULL)) {
  44.                 error("Out of memory!");
  45.                 return;
  46.             }
  47.             strcpy(buf, &prlnbuf[SFIELD]);
  48.             ptr->next = NULL;
  49.             ptr->data = buf;
  50.             if (mlptr)
  51.                 mlptr->next = ptr;
  52.             else
  53.                 mptr->line = ptr;
  54.             mlptr = ptr;
  55.         }
  56.         return;
  57.     }
  58.  
  59.     /* -- Comment line. */
  60.  
  61.     c = prlnbuf[SFIELD];
  62.     if (c == ';' || c == '*' || c == '\0') {
  63.         if (pass == LAST_PASS) {
  64.             println();
  65.             return;
  66.         }
  67.     }
  68.  
  69.     /* -- Search for a label. */
  70.  
  71.     i = SFIELD;
  72.     j = 0;
  73.     lablptr = NULL;
  74.  
  75.     while (isspace(prlnbuf[i]))
  76.         i++;
  77.     while (c = prlnbuf[i + j]) {
  78.         if ((c == '_') || (c == '.') || isalpha(c) || (isdigit(c) && (j >= 1)));
  79.         else break;
  80.         j++;
  81.     }
  82.     if ((j == 0) || ((i != SFIELD) && (c != ':')))
  83.         i = SFIELD;
  84.     else {
  85.         if (colsym(&i) != 0)
  86.             if ((lablptr = stlook()) == NULL)
  87.                 return;
  88.         if ((lablptr) && (prlnbuf[i] == ':'))
  89.             i++;
  90.     }
  91.     while (isspace(prlnbuf[i]))
  92.         i++;
  93.  
  94.     /* -- Test if a macro. */
  95.  
  96.     if (flag = macro_look(i)) {
  97.         if (flag < 0)
  98.             return;
  99.         labldef(loccnt, 1);
  100.         if (pass == LAST_PASS) {
  101.             if (!mlist)
  102.                 loadlc((page << 13) + loccnt, 0);
  103.             println();
  104.         }
  105.         mcntmax++;
  106.         mcounter = mcntmax;
  107.         expand_macro = 1;
  108.         mlptr = mptr->line;
  109.         return;
  110.     }
  111.  
  112.     /* -- Test if an instruction. */
  113.  
  114.     if ((flag = oplook(&i)) < 0) {
  115.         labldef(loccnt, 1);
  116.         if (flag == -1)
  117.             error("Invalid instruction!");
  118.         if ((flag == -2) && (pass == LAST_PASS)) {
  119.             if (lablptr)
  120.                 loadlc((page << 13) + loccnt, 1);
  121.             println();
  122.         }
  123.         return;
  124.     }
  125.  
  126.     /* -- Generate code. */
  127.  
  128.     if (opflg == PSEUDO)
  129.         pseudo(&i);
  130.     else if (labldef(loccnt, 1) == -1)
  131.         return;
  132.     else {
  133.         if (opflg == CLASS1)
  134.             class1(&i);
  135.         else if (opflg == CLASS2)
  136.             class2(&i);
  137.         else if (opflg == CLASS3)
  138.             class3(&i);
  139.         else if (opflg == CLASS5)
  140.             class5(&i);
  141.         else if (opflg == CLASS6)
  142.             class6(&i);
  143.         else if (opflg == CLASS7)
  144.             class7(&i);
  145.         else if (opflg == CLASS8)
  146.             class8(&i);
  147.         else
  148.             class4(&i);
  149.     }
  150. }
  151.  
  152. /****************************************************************************/
  153.  
  154. /*  operation code table lookup
  155.  *    if found, return pointer to symbol,
  156.  *    else, return -1
  157.  */
  158.  
  159. oplook(int *idx)
  160. {
  161.     char c, opcode[16];
  162.     int    i;
  163.  
  164.     i = 0;
  165.     while(c = prlnbuf[*idx]) {
  166.         if (c == ' ' || c == '\t' || c == ';')
  167.             break;
  168.         if (!isalnum(c) && c != '.' && c != '*' && c != '=')
  169.             return(-1);
  170.         opcode[i++] = c;
  171.         (*idx)++;
  172.         if (c == '=')
  173.             break;
  174.         if (i >= 9)
  175.             return(-1);
  176.     }
  177.  
  178.     opcode[i] = '\0';
  179.  
  180.     if (i == 0)
  181.         return (-2);
  182.  
  183.     i = 0;
  184.     while (optab[i].name) {
  185.         if (!stricmp(opcode, optab[i].name)) {
  186.             opflg = optab[i].flag;
  187.             opval = optab[i].value;
  188.             optype = optab[i].type_idx;
  189.             return(i);
  190.         }
  191.         i++;
  192.     }
  193.  
  194.     return(-1);
  195. }
  196.  
  197. /* check the end of line for garbage */
  198.  
  199. check_eol(int *ip)
  200. {
  201.     while (isspace(prlnbuf[*ip]))
  202.         (*ip)++;
  203.     if (prlnbuf[*ip] == ';' || prlnbuf[*ip] == '\0')
  204.         return (1);
  205.     else {
  206.         error("Syntax error!");
  207.         return (0);
  208.     }
  209. }
  210.  
  211.